package com.alex.demo.springtest.processor;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import lombok.Getter;

/**
 * @Version 1.0
 * @Author Alex
 * @Created Dec 2022/8/18 18:00
 * @Description
 *              <p>
 *              InitializingBean 是Bean的初始化方法接口
 *              DisposableBean 是Bean的销毁方法接口
 */
public class MyJavaBean implements InitializingBean, DisposableBean, BeanNameAware {

	@Getter
	private String desc;

	@Getter
	private String remark;

	public MyJavaBean() {
		System.out.println("this is MyJavaBean");
	}

	public void setDesc(String desc) {
		System.out.println("调用setDesc方法");
		this.desc = desc;
	}

	public void setRemark(String remark) {
		System.out.println("调用setRemark方法");
		this.remark = remark;
	}

	@PostConstruct
	public void postConstruct() {
		System.out.println("调用 MyJaveBean-postConstruct 方法");
	}

	/**
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("调用 InitializingBean接口 - afterPropertiesSet 方法");
		// this.desc = "在初始化方法中 修改后的描述信息";
	}

	public void initMethod() {
		System.out.println("调用 MyJavaBean-initMethod 方法");
	}

	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[描述：").append(desc);
		builder.append("， 备注：").append(remark).append("]");
		return builder.toString();
	}

	/**
	 * @see org.springframework.beans.factory.DisposableBean#destroy
	 *      若Bean实现了 DisposableBean 接口，则销毁时会调用该方法
	 */
	@Override
	public void destroy() throws Exception {
		System.out.println("调用 DisposableBean接口 - destory 方法");
	}

	/**
	 * 若beanDefinition中定义了 destroy-method 属性，则销毁时会调用该方法
	 */
	public void destroyMethod() {
		System.out.println("调用 MyJaveBean-destoryMethod 方法");
	}

	/**
	 * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
	 */
	@Override
	public void setBeanName(String name) {
		System.out.println("调用 BeanNameAware接口 - setBeanName 方法 " + name);
	}
}